Developer Documentation

QuickTime 4 API Documentation

Inside Macintosh: Imaging with QuickDraw

Previous | Chapter Top | Chapter Contents | Next |

Creating, Altering, and Disposing of Offscreen Graphics Worlds

To create an offscreen graphics world, use the NewGWorld function. The NewGWorld function uses the NewScreenBuffer function to create and allocate memory for an offscreen pixel image; your application generally won't need to use NewScreenBuffer , but it is described here for completeness. The NewGWorld function similarly uses the NewTempScreenBuffer function to create and allocate temporary memory for an offscreen pixel image.

To change the pixel depth, boundary rectangle, or color table for an existing offscreen graphics world, use the UpdateGWorld function.

When you no longer need the pixel image associated with this offscreen graphics world, use the DisposeGWorld procedure to dispose of all the memory allocated for the offscreen graphics world. The DisposeGWorld procedure uses the DisposeScreenBuffer procedure when disposing of an offscreen graphics world; generally, your application won't need to use DisposeScreenBuffer .

Before drawing into an offscreen graphics world, be sure to use the SetGWorld procedure (described on SetGWorld ) to make that offscreen world the current graphics port. In addition, before drawing into--or copying from--an offscreen pixel map, be sure to use the LockPixels function, which is described on LockPixels .

NewGWorld

Use the NewGWorld function to create an offscreen graphics world.

FUNCTION NewGWorld (VAR offscreenGWorld: GWorldPtr;
                                          pixelDepth: Integer; boundsRect: Rect;
                                          cTable: CTabHandle; aGDevice: GDHandle;
                                          flags: GWorldFlags): QDErr;
offscreenGWorld
A pointer to the offscreen graphics world created by this routine.
pixelDepth
The pixel depth of the offscreen world; possible depths are 1, 2, 4, 8, 16, and 32 bits per pixel. If you specify 0 in this parameter, you get the default behavior for the NewGWorld function--that is, it uses the pixel depth of the screen with the greatest pixel depth from among all screens whose boundary rectangles intersect the rectangle that you specify in the boundsRect parameter. If you specify 0 in this parameter, NewGWorld also uses the GDevice record from this device instead of creating a new GDevice record for the offscreen world. If you use NewGWorld on a computer that supports only basic QuickDraw, you may specify only 0 or 1 in this parameter.
boundsRect
The boundary rectangle and port rectangle for the offscreen pixel map. This becomes the boundary rectangle for the GDevice record, if  NewGWorld creates one. If you specify 0 in the pixelDepth parameter, NewGWorld interprets the boundaries in global coordinates that it uses to determine which screens intersect the rectangle. ( NewGWorld then uses the pixel depth, color table, and GDevice record from the screen with the greatest pixel depth from among all screens whose boundary rectangles intersect this rectangle.) Typically, your application supplies this parameter with the port rectangle for the onscreen window into which your application will copy the pixel image from this offscreen world.
cTable
A handle to a ColorTable record. If you pass NIL in this parameter, NewGWorld uses the default color table for the pixel depth that you specify in the pixelDepth parameter. If you set the pixelDepth parameter to 0, NewGWorld ignores the cTable parameter and instead copies and uses the color table of the graphics device with the greatest pixel depth among all graphics devices whose boundary rectangles intersect the rectangle that you specify in the boundsRect parameter. If you use NewGWorld on a computer that supports only basic QuickDraw, you may specify only NIL in this parameter.
aGDevice
A handle to a GDevice record that is used only when you specify the noNewDevice flag in the flags parameter, in which case NewGWorld attaches this GDevice record to the new offscreen graphics world. If you set the pixelDepth parameter to 0, or if you do not set the noNewDevice flag, NewGWorld ignores the aGDevice parameter, so you should set it to NIL . If you set the pixelDepth parameter to 0, NewGWorld uses the GDevice record for the graphics device with the greatest pixel depth among all graphics devices whose boundary rectangles intersect the rectangle that you specify in the boundsRect parameter. You should pass NIL in this parameter if the computer supports only basic QuickDraw. Generally, your application should never create GDevice records for offscreen graphics worlds.
Note: QuickTime 3 for Windows does not support the noNewDevice flag.

flags
Options available to your application. You can set a combination of the flags pixPurge , noNewDevice , useTempMem , and keepLocal . If you don't wish to use any of these flags, pass the empty set ([ ]) in your Pascal code or 0 in your C code in this parameter, in which case you get the default behavior for NewGWorld --that is, it creates an offscreen graphics world where the base address for the offscreen pixel image is unpurgeable, it uses an existing GDevice record (if you pass 0 in the depth parameter) or creates a new GDevice record, it uses memory in your application heap, and it allows graphics accelerators to cache the offscreen pixel image. The available flags are described here:
Note: QuickTime 3 for Windows does not support the noNewDevice flag.

TYPE GWorldFlags =
SET OF (            {flags for only NewGWorld are listed here}
pixPurge,          {make base address for offscreen pixel }
                    { image purgeable}
noNewDevice,       {do not create an offscreen GDevice }
                    { record}
useTempMem,        {create base address for offscreen pixel }
                    { image in temporary memory}
keepLocal,         {keep offscreen pixel image in main }
                    { memory where it cannot be cached to }
                    { a graphics accelerator card}
);

DESCRIPTION

The NewGWorld function creates an offscreen graphics world with the pixel depth you specify in the pixelDepth parameter, the boundary rectangle you specify in the boundsRect parameter, the color table you specify in the cTable parameter, and the options you specify in the flags parameter. The NewGWorld function returns a pointer to the new offscreen graphics world in the offscreenGWorld parameter. You use this pointer when referring to this new offscreen world in other routines described in this chapter.

Typically, you pass 0 in the pixelDepth parameter, a window's port rectangle in the boundsRect parameter, NIL in the cTable and aGDevice parameters, and--in the flags parameter--an empty set ([ ]) for Pascal code or 0 for C code. This provides your application with the default behavior of NewGWorld , and it supports computers running basic QuickDraw. This also allows QuickDraw to optimize the CopyBits , CopyMask , and CopyDeepMask procedures when your application copies the image in an offscreen graphics world into an onscreen graphics port.

The NewGWorld function allocates memory for an offscreen graphics port and its pixel map. On computers that support only basic QuickDraw, NewGWorld creates a 1-bit pixel map that your application can manipulate using other relevant routines described in this chapter. Your application can copy this 1-bit pixel map into basic graphics ports.

Unless you specify 0 in the pixelDepth parameter--or pass the noNewDevice flag in the flags parameter and supply a GDevice record in the aGDevice parameter-- NewGWorld also allocates a new offscreen GDevice record.

When creating an image, your application can use the NewGWorld function to create an offscreen graphics world that is optimized for an image's characteristics--for example, its best pixel depth. After creating the image, your application can then use the CopyBits , CopyMask , or CopyDeepMask procedure to copy that image to an onscreen graphics port. Color QuickDraw automatically renders the image at the best available pixel depth for the screen. Creating an image in an offscreen graphics port and then copying it to the screen in this way prevents the visual choppiness that would otherwise occur if your application were to build a complex image directly onscreen.

The NewGWorld function initializes the offscreen graphics port by calling the OpenCPort function. The NewGWorld function sets the offscreen graphics port's visible region to a rectangular region coincident with its boundary rectangle. The NewGWorld function generates an inverse table with the Color Manager procedure MakeITable , unless one of the GDevice records for the screens has the same color table as the GDevice record for the offscreen world, in which case NewGWorld uses the inverse table from that GDevice record.

The address of the offscreen pixel image is not directly accessible from the PixMap record for the offscreen graphics world. However, you can use the GetPixBaseAddr function (described on GetPixBaseAddr ) to get a pointer to the beginning of the offscreen pixel image.

For purposes of estimating memory use, you can compute the size of the offscreen pixel image by using this formula:

rowBytes * (boundsRect.bottom - boundsRect.top)

In the flags parameter, you can specify several options that are defined by the GWorldFlags data type. If you don't wish to use any of these options, pass an empty set ([ ]) in the flags parameter for Pascal code or pass 0 here for C code.

As its function result, NewGWorld returns one of three result codes.

SPECIAL CONSIDERATIONS

If you supply a handle to a ColorTable record in the cTable parameter, NewGWorld makes a copy of the record and stores its handle in the offscreen PixMap record. It is your application's responsibility to make sure that the ColorTable record you specify in the cTable parameter is valid for the offscreen graphics port's pixel depth.

If when using NewGWorld you specify a pixel depth, color table, or GDevice record that differs from those used by the window into which you copy your offscreen image, the CopyBits , CopyMask , and CopyDeepMask procedures require extra time to complete.

To use a custom color table in an offscreen graphics world, you need to create the associated offscreen GDevice record, because Color QuickDraw needs its inverse table.

The NewGWorld function may move or purge memory blocks in the application heap. Your application should not call this function at interrupt time.

ASSEMBLY-LANGUAGE INFORMATION

The trap macro and routine selector for the NewGWorld function are

Trap macro

Selector

_QDExtensions

$00160000

RESULT CODES

noErr

0

No error

paramErr

-50

Illegal parameter

cDepthErr

-157

Invalid pixel depth

SEE ALSO

Listing 6-1 and Listing 6-2 illustrate how to use NewGWorld to create offscreen graphics worlds.

If your application needs to change the pixel depth, boundary rectangle, or color table for an offscreen graphics world, use the UpdateGWorld function, described on UpdateGWorld .

NewScreenBuffer

The NewGWorld function uses the NewScreenBuffer function to create an offscreen PixMap record and allocate memory for the base address of its pixel image; applications generally don't need to use NewScreenBuffer .

FUNCTION NewScreenBuffer (globalRect: Rect;
                                          purgeable: Boolean; VAR gdh: GDHandle;
                                          VAR offscreenPixMap: PixMapHandle):
                                          QDErr;
globalRect
The boundary rectangle, in global coordinates, for the offscreen pixel map.
purgeable
A value of TRUE to make the memory block for the offscreen pixel map purgeable, or a value of FALSE to make it unpurgeable.
gdh
The handle to the GDevice record for the graphics device with the greatest pixel depth among all graphics devices whose boundary rectangles intersect the rectangle specified in the globalRect parameter.
offscreenPixMap
A handle to the new offscreen PixMap record.

DESCRIPTION

The NewScreenBuffer function creates a new offscreen PixMap record, using the pixel depth and color table of the device whose GDevice record is returned in the gdh parameter. The NewScreenBuffer function returns a handle to the new offscreen pixel map in the offscreenPixMap parameter.

As its function result, NewScreenBuffer returns one of three result codes.

SPECIAL CONSIDERATIONS

The NewScreenBuffer function may move or purge memory blocks in the application heap. Your application should not call this function at interrupt time.

ASSEMBLY-LANGUAGE INFORMATION

The trap macro and routine selector for the NewScreenBuffer function are

Trap macro

Selector

_QDExtensions

$000E0010

RESULT CODES

noErr

0

No error

paramErr

-50

Illegal parameter

cNoMemErr

-152

Failed to allocate memory for structures

NewTempScreenBuffer

The NewGWorld function uses the NewTempScreenBuffer function to create an offscreen PixMap record and allocate temporary memory for the base address of its pixel image; applications generally don't need to use NewTempScreenBuffer .

FUNCTION NewTempScreenBuffer (globalRect: Rect;
                                          purgeable: Boolean;
                                          VAR gdh: GDHandle;
                                          VAR offscreenPixMap: PixMapHandle):
                                          QDErr;
globalRect
The boundary rectangle, in global coordinates, for the offscreen pixel map.
purgeable
A value of TRUE to make the memory block for the offscreen pixel map purgeable, or a value of FALSE to make it unpurgeable.
gdh
The handle to the GDevice record for the graphics device with the greatest pixel depth among all graphics devices whose boundary rectangles intersect the rectangle specified in the globalRect parameter.
offscreenPixMap
A handle to the new offscreen PixMap record.

DESCRIPTION

The NewTempScreenBuffer function performs the same functions as NewScreenBuffer except that it creates the base address for the offscreen pixel image in temporary memory. When an application passes it the useTempMem flag, the  NewGWorld function uses NewTempScreenBuffer instead of NewScreenBuffer .

SPECIAL CONSIDERATIONS

The NewTempScreenBuffer function may move or purge memory blocks in the application heap. Your application should not call this function at interrupt time.

ASSEMBLY-LANGUAGE INFORMATION

The trap macro and routine selector for the NewTempScreenBuffer function are

Trap macro

Selector

_QDExtensions

$000E0015

UpdateGWorld

To change the pixel depth, boundary rectangle, or color table for an existing offscreen graphics world, use the UpdateGWorld function. You should call UpdateGWorld after every update event and whenever your windows move or change size.

FUNCTION UpdateGWorld (VAR offscreenGWorld: GWorldPtr;
                                          pixelDepth: Integer; boundsRect: Rect;
                                          cTable: CTabHandle; aGDevice: GDHandle;
                                          flags: GWorldFlags): GWorldFlags;
offscreenGWorld
On input, a pointer to an existing offscreen graphics world; upon completion, the pointer to the updated offscreen graphics world.
pixelDepth
The pixel depth of the offscreen world; possible depths are 1, 2, 4, 8, 16, and 32 bits per pixel. If you specify 0 in this parameter, UpdateGWorld rescans the device list and uses the depth of the screen with the greatest pixel depth among all screens whose boundary rectangles intersect the rectangle that you specify in the boundsRect parameter. If you specify 0 in this parameter, UpdateGWorld also copies the GDevice record from this device to create an offscreen GDevice record. The UpdateGWorld function ignores the value you supply for this parameter if you specify a GDevice record in the aGDevice parameter.
boundsRect
The boundary rectangle and port rectangle for the offscreen pixel map. This also becomes the boundary rectangle for the GDevice record, if  NewGWorld creates one. If you specify 0 in the pixelDepth parameter, NewGWorld interprets the boundaries in global coordinates, with which it determines which screens intersect the rectangle. ( NewGWorld then uses the pixel depth, color table, and GDevice record from the screen with the greatest pixel depth from among all screens whose boundary rectangles intersect this rectangle.) Typically, your application supplies this parameter with the port rectangle for the onscreen window into which your application will copy the pixel image from this offscreen world.
cTable
A handle to a ColorTable record. If you pass NIL in this parameter, UpdateGWorld uses the default color table for the pixel depth that you specify in the pixelDepth parameter; if you set the pixelDepth parameter to 0, UpdateGWorld copies and uses the color table of the graphics device with the greatest pixel depth among all graphics devices whose boundary rectangles intersect the rectangle that you specify in the boundsRect parameter. The UpdateGWorld function ignores the value you supply for this parameter if you specify a GDevice record in the aGDevice parameter.
aGDevice
As an option, a handle to a GDevice record whose pixel depth and color table you want to use for the offscreen graphics world. To use the pixel depth and color table that you specify in the pixelDepth and cTable parameters, set this parameter to NIL .
flags
Options available to your application. You can set a combination of the flags keepLocal , clipPix , stretchPix , and ditherPix . If you don't wish to use any of these flags, pass the empty set ([ ]) in this parameter for Pascal code or pass 0 here for C code. However, you should pass either clipPix or stretchPix to ensure that the pixel map is updated to reflect the new color table. The available flags are described here:
             TYPE GWorldFlags =
             SET OF (           {flags for UpdateGWorld are listed here}
             keepLocal,        {keep data structures in main memory}
             clipPix,          {update and clip pixel image to new }
                                 { boundary rectangle}
             stretchPix,       {update and stretch or shrink pixel }
                                 { image to the new boundary rectangle}
             ditherPix,        {include with clipPix or stretchPix }
                                 { flag to dither the pixel image}
             );

DESCRIPTION

The UpdateGWorld function changes an offscreen graphics world to the specified pixel depth, rectangle, color table, and options that you supply in the pixelDepth , boundsRect , cTable , and flags parameters, respectively. In the offscreenGWorld parameter, pass the pointer returned to your application by the NewGWorld function when you created the offscreen graphics world.

If the LockPixels function (described on LockPixels ) reports that the Memory Manager has purged the base address for the offscreen pixel image, you can use UpdateGWorld to reallocate its memory. Your application should then reconstruct the pixel image or draw directly in a window instead of preparing the image in an offscreen graphics world.

In the flags parameter, you can specify the keepLocal flag, which keeps the offscreen pixel image in Macintosh main memory or returns the image to main memory if it had been previously cached. If you use UpdateGWorld without passing it the keepLocal flag, you allow the offscreen pixel image to be cached on a graphics accelerator card if one is present.

As its function result, UpdateGWorld returns the gwFlagErr flag if UpdateGWorld was unsuccessful; in this case, the offscreen graphics world is left unchanged. You can use the QDError function, described in the chapter "Color QuickDraw," to help you determine why UpdateGWorld failed.

If UpdateGWorld is successful, it returns a combination of the following flags, which are defined by the GWorldFlags data type:

Flag

Meaning

mapPix

Color QuickDraw remapped the colors to a new color table.

newDepth

Color QuickDraw translated the pixel values in the offscreen pixel image to those for a different pixel depth.

alignPix

QuickDraw realigned the offscreen image to the window.

newRowBytes

QuickDraw changed the value of the rowBytes field in the PixMap record for the offscreen graphics world.

reallocPix

QuickDraw had to reallocate memory for the offscreen pixel image; your application should then reconstruct the pixel image, or draw directly in a window instead of preparing the image in an offscreen graphics world.

clipPix

QuickDraw clipped the pixel image.

stretchPix

QuickDraw stretched or shrank the offscreen image.

ditherPix

Color QuickDraw dithered the offscreen pixel image.

The UpdateGWorld function uses the following algorithm when updating the offscreen pixel image:

  1. If the color table that you specify in the cTable parameter is different from the previous color table, or if the color table associated with the GDevice record that you specify in the aGDevice parameter is different, Color QuickDraw maps the pixel values in the offscreen pixel map to the new color table.
  2. If the value you specify in the pixelDepth parameter differs from the previous pixel depth, Color QuickDraw translates the pixel values in the offscreen pixel image to those for the new pixel depth.
  3. If the rectangle you specify in the boundsRect parameter differs from, but has the same size as, the previous boundary rectangle, QuickDraw realigns the pixel image to the screen for optimum performance for the CopyBits procedure.
  4. If the rectangle you specify in the boundsRect parameter is smaller than the previous boundary rectangle and you specify the clipPix flag, the pixel image is clipped along the bottom and right edges.
  5. If the rectangle you specify in the boundsRect parameter is bigger than the previous boundary rectangle and you specify the clipPix flag, the bottom and right edges of the pixel image are undefined.
  6. If the rectangle you specify in the boundsRect parameter is smaller than the previous boundary rectangle and you specify the stretchPix flag, the pixel image is reduced to the new size.
  7. If the rectangle you specify in the boundsRect parameter is bigger than the previous boundary rectangle and you specify the stretchPix flag, the pixel image is stretched to the new size.
  8. If the Memory Manager purged the base address for the offscreen pixel image, UpdateGWorld reallocates the memory, but the pixel image is lost. You must reconstruct it.

SPECIAL CONSIDERATIONS

The UpdateGWorld function may move or purge memory blocks in the application heap. Your application should not call this function at interrupt time.

ASSEMBLY-LANGUAGE INFORMATION

The trap macro and routine selector for the UpdateGWorld function are

Trap macro

Selector

_QDExtensions

$00160003

DisposeGWorld

Use the DisposeGWorld procedure to dispose of all the memory allocated for an offscreen graphics world.

PROCEDURE DisposeGWorld (offscreenGWorld: GWorldPtr);
offscreenGWorld
A pointer to an offscreen graphics world.

DESCRIPTION

The DisposeGWorld procedure disposes of all the memory allocated for the offscreen graphics world pointed to in the offscreenGWorld parameter, including its pixel map, color table, pixel image, and GDevice record (if one was created). In the offscreenGWorld parameter, pass the pointer returned to your application by the NewGWorld function when you created the offscreen graphics world.

Call DisposeGWorld only when your application no longer needs the pixel image associated with this offscreen graphics world. If this offscreen graphics world was the current device, the current device is reset to the device stored in the global variable MainDevice .

ASSEMBLY-LANGUAGE INFORMATION

The trap macro and routine selector for the DisposeGWorld procedure are

Trap macro

Selector

_QDExtensions

$00040004

DisposeScreenBuffer

The DisposeGWorld procedure uses the DisposeScreenBuffer procedure when disposing of an offscreen graphics world; generally, applications do not need to use DisposeScreenBuffer .

PROCEDURE DisposeScreenBuffer (offscreenPixMap: PixMapHandle);
offscreenPixMap
A handle to an existing offscreen PixMap record.

DESCRIPTION

The DisposeScreenBuffer procedure disposes of the memory allocated for the base address of an offscreen pixel image.

ASSEMBLY-LANGUAGE INFORMATION

The trap macro and routine selector for the DisposeScreenBuffer procedure are

Trap macro

Selector

_QDExtensions

$00040011


© 1997 Apple Computer, Inc.

Previous | Chapter Top | Chapter Contents | Next